package com.wen.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.edu.entity.EduTeacher;
import com.wen.edu.mapper.EduTeacherMapper;
import com.wen.edu.service.EduTeacherService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author wen
 * @since 2021-05-26
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    /**
     * 查询前台显示的名师列表
     *
     * @return list of required
     */
    @Override
    @Cacheable(value = "teachers", key = "'selectFamousTeacher'")
    public List<EduTeacher> selectFamousTeacher() {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 4");
        return baseMapper.selectList(wrapper);
    }

    /**
     * 分页查询前台讲师数据
     *
     * @param pageTeacher 前台讲师分页查询结果
     * @return resultEntity
     */
    @Override
    public Map<String, Object> pageFrontTeacher(Page<EduTeacher> pageTeacher) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        baseMapper.selectPage(pageTeacher, wrapper);

        List<EduTeacher> records = pageTeacher.getRecords();// 每页显示的数量
        long total = pageTeacher.getTotal();// 总条数
        long pages = pageTeacher.getPages();//当前分页总页数
        long size = pageTeacher.getSize();//每页条数
        long current = pageTeacher.getCurrent();//当前页
        boolean hasNext = pageTeacher.hasNext();
        boolean hasPrevious = pageTeacher.hasPrevious();

        Map<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
